<?php

namespace App\Exports;

use App\Models\Item;
use App\Models\ItemFee;
use App\Repos\ItemRepo;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;

class ItemsExport implements FromView
{
    use Exportable;
    public function __construct($where)
    {

        $this->where = $where;
    }

    public function view(): View
    {
    	$q = ItemFee::query();
       
        if(array_get($this->where,'start_date')) {
            $begin = array_get($this->where,'start_date');
            $end   = array_get($this->where,'end_date');
            $q->whereHas('item', function ($q) use ($begin,$end) {
                $q->whereBetween('created_at', [$begin,$end]);
            });
           
        }

        



        if(array_get($this->where,'code')) {
            $code = array_get($this->where,'code');
            $q->whereHas('item', function ($q) use ($code) {
                $q->where('code', 'like', '%'.$code.'%');
            });
        }

        if(array_get($this->where,'ship_name')) {
            $ship_name = array_get($this->where,'ship_name');
            $q->whereHas('ship_name', function ($q) use ($ship_name) {
                $q->where('ship_name', 'like', '%'.$ship_name.'%');
            });
        }

        if(array_get($this->where,'ship_line')) {
            $ship_line = array_get($this->where,'ship_line');
            $q->whereHas('item', function ($q) use ($ship_line) {
                $q->where('ship_line', 'like', '%'.$ship_line.'%');
            });
        }

        if(array_get($this->where,'contacter')) {
            $contacter   = array_get($this->where,'contacter');
             $money_type = array_get($this->where,'money_type');
             if($money_type=='receive') {
                $q->where('receive_contacter','like','%'.$contacter.'%');   
             }
             if($money_type=='pay') {
                $q->where('pay_contacter','like','%'.$contacter.'%');   
             }
            
        }
        
    	$fees  = $q->select('fee_name')->groupBy('fee_name')->get(); 
        
        


        $query = Item::query();
        if(array_get($this->where,'code')) {
            $code = array_pull($this->where,'code');           
            $query->where('code', 'like', '%'.$code.'%');            
        }

        if(array_get($this->where,'ship_name')) {
            $ship_name = array_pull($this->where,'ship_name');           
            $query->where('ship_name', 'like', '%'.$ship_name.'%');            
        }

        if(array_get($this->where,'ship_line')) {
            $ship_line = array_pull($this->where,'ship_line');           
            $query->where('ship_line', 'like', '%'.$ship_line.'%');            
        }

        if(array_get($this->where,'start_date')) {
            $begin = array_pull($this->where,'start_date');
            $end   = array_pull($this->where,'end_date');           
            $query->whereBetween('created_at', [$begin,$end]);          
        }
        $contacter = '';
        if(array_get($this->where,'contacter')) {
            $contacter = array_pull($this->where,'contacter');
            $query->whereHas('fee', function ($query) use ($contacter) {
                if(array_get($this->where,'money_type')=='receive') {
                    $query->where('receive_contacter','like','%'.$contacter.'%');
                }
                if(array_get($this->where,'money_type')=='pay') {
                    $query->where('pay_contacter','like','%'.$contacter.'%');    
                }
                
            });
        }

        if(array_get($this->where,'money_type')=='pay') {
            $fees->where('pay_money','!=','');
             $query->whereHas('fee', function ($query)  {
                $query->where('pay_money','!=','');
            });
        }

        if(array_get($this->where,'money_type')=='receive') {           
             $query->whereHas('fee', function ($query)  {
                $query->where('receive_money','!=','');
            });
        }

        if(array_get($this->where,'money_type')=='paied') {
           
             $query->whereHas('fee', function ($query)  {
                $query->where('pay_status',1);
            });
        }

        if(array_get($this->where,'money_type')=='received') {
           
             $query->whereHas('fee', function ($query)  {
                $query->where('receive_status',1);
            });
        }




        

    	$list = $query->get();

        $tplArr['receive']  = 'receive';
        $tplArr['pay']      = 'pay';
        $tplArr['received'] = 'receive'; 
        $tplArr['paied']    = 'pay';
    	$tpl  = $tplArr[$this->where['money_type']];

        $item_ids = $list->pluck('id');
        
        $feeTotal = [];
        foreach ($fees as $fee) {
            $feeTotal[$fee->fee_name] = ItemRepo::totalByFeename($item_ids,$fee->fee_name,$this->where['money_type'],$contacter);
        }
        
        return view('hrs.export.'.$tpl, compact('fees','list','feeTotal','contacter'));
    }
}
